import { Controller } from "../../mvc/core/Controller";
import { SoundManager } from "../../common/sound/SoundManager";
import { SoundTypeConst } from "../../common/sound/SoundTypeConst";
import { LoaderManager } from "../../../com/gengine/resource/LoaderManager";
import { SWFInfo } from "../../../com/gengine/resource/info/SWFInfo";
import { Global } from "../../../com/gengine/global/Global";
import { ExpAddInfo } from "../view/effect/ExpAddInfo";
import { UpdateCode } from "../../component/gconst/UpdateCode";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { GameController } from "../mvc/GameController";
import { ExpAddEffect } from "../view/effect/ExpAddEffect";
import { ExpAddEffectInfo } from "../resource/info/ExpAddEffectInfo";
import { DataEvent } from "../events/DataEvent";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
type int = number;
//class EffectController
    
    export  class EffectController extends Controller
    {
       

        /* internal  */onTaskGetHandler(arg1: Object): void
        {
            this.loadEffect("gettask.swf");
            SoundManager.instance.soundPlay(SoundTypeConst.AcceptTask);
            return;
        }

        /* internal  */onTaskEndHandler(arg1: Object): void
        {
            this.loadEffect("endtask.swf");
            SoundManager.instance.soundPlay(SoundTypeConst.CompleteTask);
            return;
        }

        /* internal  */loadEffect(arg1: string): void
        {
            LoaderManager.instance.load(arg1, this.onEffectLoaded);
            return;
        }

        /* internal  */onEffectLoaded(arg1: SWFInfo): void
        {
            if (this._movieEffct && this._movieEffct.parent) 
            {
                this._movieEffct.removeEventListener(flash.events.Event.ENTER_FRAME, this.onMovieEffectEnterFrame);
                this._movieEffct.parent.removeChild(this._movieEffct);
            }
            this._movieEffct = arg1.clip;
            this._movieEffct.scaleX = 1.5;
            this._movieEffct.scaleY = 1.5;
            this._movieEffct.mouseEnabled = false;
            this._movieEffct.mouseChildren = false;
            Global.stage.addChild(this._movieEffct);
            this._movieEffct.addEventListener(flash.events.Event.ENTER_FRAME, this.onMovieEffectEnterFrame);
            this.stageResize();
            this._movieEffct.gotoAndPlay(1);
            return;
        }

        /* internal  */onMovieEffectEnterFrame(arg1: Object /* flash.events.Event */): void
        {
            var loc1;
            var loc2=((loc1 = this)._count + 1);
            loc1._count = loc2;
            if (this._count % 2 == 0) 
            {
                this._movieEffct.nextFrame();
                this._movieEffct.nextFrame();
            }
            if (this._movieEffct.currentFrame == this._movieEffct.totalFrames) 
            {
                this._movieEffct.removeEventListener(flash.events.Event.ENTER_FRAME, this.onMovieEffectEnterFrame);
                if (this._movieEffct.parent) 
                {
                    this._movieEffct.parent.removeChild(this._movieEffct);
                }
                this._count = 0;
            }
            return;
        }

        public stageResize(): void
        {
            if (this._movieEffct && this._movieEffct.parent) 
            {
                this._movieEffct.x = (Global.stage.stageWidth - 290 * 1.5) / 2;
                this._movieEffct.y = 30;
            }
            return;
        }

        /* internal  */onRoleExpAddHandler(arg1: ExpAddInfo): void
        {
            var loc1=this.isShowEffect(arg1);
            var loc2=arg1.valueAdd * cache.role.exPerent;
            if (loc1 > 0) 
            {
                if (arg1.code != UpdateCode.EUpdateCodeMergeCopy) 
                {
                    this.playExpAddEffect(arg1.valueAdd, loc1);
                    if (cache.role.getWorldExAble()) 
                    {
                        flash.utils.setTimeout(this.playExpAddEffect, 500, loc2, loc1);
                    }
                }
                else 
                {
                    flash.utils.setTimeout(this.playExpAddEffect, 500, arg1.valueAdd, loc1);
                    if (cache.role.getWorldExAble()) 
                    {
                        flash.utils.setTimeout(this.playExpAddEffect, 1000, loc2, loc1);
                    }
                }
            }
            return;
        }

        /* internal  */playExpAddEffect(arg1: int, arg2: int): void
        {
            var loc1=RolePlayer.instance.stagePointX;
            var loc2=RolePlayer.instance.stagePointY - 55;
            var loc3=GameController.gameui.getGameui().navbar.x + 115 + 357;
            var loc4=GameController.gameui.getGameui().navbar.y - 10;
            var loc5;
            (loc5 = new ExpAddEffect(new ExpAddEffectInfo(loc1, loc2, loc3, loc4, arg1, arg2))).playEffect();
            return;
        }

        /* internal  */isShowEffect(arg1: ExpAddInfo): int
        {
            if (arg1.code == UpdateCode.EUpdateCodeGetOfflineExp || arg1.code == UpdateCode.EUpdateBlessBottle) 
            {
                return 3;
            }
            if (arg1.code == UpdateCode.EUpdateCodeTask && cache.role.roleInfo.level < 30 && arg1.valueAdd >= 5000 && Math.random() <= 0.3) 
            {
                return 2;
            }
            if (arg1.code == UpdateCode.EUpdateCodeTask && cache.role.roleInfo.level >= 30 && arg1.valueAdd >= 80000) 
            {
                return 3;
            }
            if (arg1.code == UpdateCode.EUpdateCodeTask && cache.role.roleInfo.level >= 30 && arg1.valueAdd >= 40000) 
            {
                return 2;
            }
            if (arg1.code == UpdateCode.EUpdateCodeTask && cache.role.roleInfo.level >= 30 && arg1.valueAdd >= 20000) 
            {
                return 1;
            }
            if (arg1.code == UpdateCode.ExpDice || arg1.code == UpdateCode.EUpdateCodeGuildStruggle) 
            {
                return 2;
            }
            if (arg1.valueAdd >= 40000) 
            {
                return 2;
            }
            if (cache.copy.isInMagicTower() && arg1.valueAdd > 1000) 
            {
                return 1;
            }
            if (cache.copy.isInCopy && arg1.valueAdd >= 5000) 
            {
                return 1;
            }
            return 0;
        }

        /* internal  */onShowAddExpEffect(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            this.playExpAddEffect(loc1, 3);
            return;
        }

        /* internal  */onShowAddExpEffectByInfo(arg1: DataEvent): void
        {
            var loc1=arg1.data as ExpAddEffectInfo;
            if (loc1.startX == 0 && loc1.startY == 0) 
            {
                loc1.startX = RolePlayer.instance.stagePointX;
                loc1.startY = RolePlayer.instance.stagePointY - 55;
            }
            var loc2=new ExpAddEffect(loc1);
            loc2.playEffect();
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            NetDispatcher.addCmdListener(ServerCommand.TaskGetRes, this.onTaskGetHandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskEndRes, this.onTaskEndHandler);
            NetDispatcher.addCmdListener(ServerCommand.RoleExpAdd, this.onRoleExpAddHandler);
            Dispatcher.addEventListener(EventName.ShowAddExpEffect, this.onShowAddExpEffect);
            Dispatcher.addEventListener(EventName.ShowAddExpEffectByInfo, this.onShowAddExpEffectByInfo);
            return;
        }

        private /* var */_movieEffct: Object /* flash.display.MovieClip */;

        private /* var */_count: int;
    }
